package com.tutu.common.pager;

import com.tutu.common.GlobalConstants;

public class Pager {
	
	private static final int DEFAULT_PAGE_SIZE = GlobalConstants.DEFAULT_PAGE_NUMBER;
    private int totalRows;
    private int pageSize;
    private int currentPage;
    private int totalPages;
    private int startRow;
    
    public Pager(int _totalRows) {
        pageSize = DEFAULT_PAGE_SIZE;
        init(_totalRows, 10, 1);
    }

    public Pager(int _totalRows, int _pageSize) {
        pageSize = DEFAULT_PAGE_SIZE;
        init(_totalRows, _pageSize, 1);
    }

    public Pager(int _totalRows, int _pageSize, int _currentPage) {
        pageSize = DEFAULT_PAGE_SIZE;
        init(_totalRows, _pageSize, _currentPage);
    }

    private void init(int _totalRows, int _pageSize, int _currentPage) {
        totalRows = _totalRows;
        pageSize = _pageSize;
        currentPage = _currentPage;
        if(totalRows < 0)
            totalRows = 0;
        if(pageSize < 1)
            pageSize = DEFAULT_PAGE_SIZE;
        totalPages = totalRows / pageSize;
        int mod = totalRows % pageSize;
        if(mod > 0)
            totalPages++;
        if(currentPage > totalPages)
            currentPage = totalPages;
        if(currentPage < 1)
            currentPage = 1;
        startRow = (currentPage - 1) * pageSize;
    }

    public int getStartRow() {
        return startRow;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalRows() {
        return totalRows;
    }
    
}
